package no.ntnu.fp.model1;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.List;

import javax.swing.*;
import javax.swing.border.TitledBorder;

public class mwsGui implements ActionListener {

	JButton checkButton, checkButton1, checkButton2, checkButton3;
	JButton checkButton4;
	JButton checkButton5;
	JButton checkButton6;
	
	JFrame frame;
	ArrayList<LWS> lws;
	MWS mws;
	
	public mwsGui(MWS m){
		mws = m;
		createGui();
	}
	
	private JPanel addPanel(String name, JButton b){
		TitledBorder title;
		JPanel panel = new JPanel(new GridLayout(0, 1));
		title = BorderFactory.createTitledBorder(name);
		panel.setBorder(title);
		JTextArea jtp1 = new JTextArea("Green: "+Math.round((Math.random()*20))); //+" "+"Yellow: "+Math.round((Math.random()*10))+" "+"Red: "+Math.round((Math.random()*4)));
		JTextArea jtp2 = new JTextArea("Yellow: "+Math.round((Math.random()*12)));
		JTextArea jtp3 = new JTextArea("Red: "+Math.round((Math.random()*4)));
		jtp1.setBackground(Color.green);
		jtp2.setBackground(Color.yellow);
		jtp3.setBackground(Color.red);
		
		b = new JButton("Check");
		b.addActionListener(this);
		panel.add(jtp1);
		panel.add(jtp2);
		panel.add(jtp3);
		panel.add(b);
		return panel;
	}
	
	private JPanel initPanel(String name){
		TitledBorder title;
		JPanel panel = new JPanel(new GridLayout(0, 1));
		title = BorderFactory.createTitledBorder(name);
		panel.setBorder(title);
		JTextArea jtp1 = new JTextArea("Green: "+Math.round((Math.random()*20))); //+" "+"Yellow: "+Math.round((Math.random()*10))+" "+"Red: "+Math.round((Math.random()*4)));
		JTextArea jtp2 = new JTextArea("Yellow: "+Math.round((Math.random()*12)));
		JTextArea jtp3 = new JTextArea("Red: "+Math.round((Math.random()*4)));
		jtp1.setBackground(Color.green);
		jtp2.setBackground(Color.yellow);
		jtp3.setBackground(Color.red);
		panel.add(jtp1);
		panel.add(jtp2);
		panel.add(jtp3);
		return panel;
	}
	
	protected void createGui(){
		frame = new JFrame("WATCH");
		frame.setResizable(false);
		//frame.setUndecorated(true);
		frame.setLocation(100, 80);
		frame.setAlwaysOnTop(true);
		JPanel panel = new JPanel(new GridLayout(0, 2));
		TitledBorder title;
		title = BorderFactory.createTitledBorder("MASTER WATCH");
		panel.setBorder(title);
		lws = mws.lwsList;
		
		 //(lws.get(0)).getGreenCount()+(lws.get(0)).getYellowCount()+(lws.get(0)).getRedCount()
		
		JPanel panel1 = initPanel("LWS1");
		checkButton1 = new JButton("Check");
		checkButton1.addActionListener(this);
		panel1.add(checkButton1);
		JPanel panel2 = initPanel("LWS2");
		checkButton2 = new JButton("Check");
		checkButton2.addActionListener(this);
		panel2.add(checkButton2);
		JPanel panel3 = initPanel("LWS3");
		checkButton3 = new JButton("Check");
		checkButton3.addActionListener(this);
		panel3.add(checkButton3);
		JPanel panel4 = initPanel("LWS4");
		checkButton4 = new JButton("Check");
		checkButton4.addActionListener(this);
		panel4.add(checkButton4);
		JPanel panel5 = initPanel("LWS5");
		checkButton5 = new JButton("Check");
		checkButton5.addActionListener(this);
		panel5.add(checkButton5);
		JPanel panel6 = initPanel("LWS6");
		checkButton6 = new JButton("Check");
		checkButton6.addActionListener(this);
		panel6.add(checkButton6);
		
		panel.add(panel1);
		panel.add(panel2);
		panel.add(panel3);
		panel.add(panel4);
		panel.add(panel5);
		panel.add(panel6);
		
		/**
		panel.add(addPanel("LWS1", checkButton1));
		panel.add(addPanel("LWS2", checkButton2));
		panel.add(addPanel("LWS3", checkButton3));
		panel.add(addPanel("LWS4", checkButton4));
		panel.add(addPanel("LWS5", checkButton5));
		panel.add(addPanel("LWS6", checkButton6));
		**/
		
		frame.setContentPane(panel);
				
		frame.pack();
		frame.setSize(600, 400);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setVisible(true);
	}
	
	/**public static void main(String[] args) {
		guiTest g = new guiTest();
		LocalWatchSystem l = new LocalWatchSystem();
		//g.addLWS(l);
		new guiTest().createGui();
	}**/

	@Override
	public void actionPerformed(ActionEvent e) {
		Object d = e.getSource();
		if(d==checkButton1){
			actionOutput(1);
		}else if(d==checkButton2){
			actionOutput(2);
		}else if(d==checkButton3){
			actionOutput(3);
		}else if(d==checkButton4){
			actionOutput(4);
		}else if(d==checkButton5){
			actionOutput(5);
		}else if(d==checkButton6){
			actionOutput(6);
		}else if(d==checkButton){
			actionOutput(9);
		}
		/**
		System.out.println("Loading Local Watch System "+nr+"..");
		frame.hide();
		new lwsGui().createGui();
		**/
	}
	
	@SuppressWarnings("deprecation")
	private void actionOutput(int i){
		System.out.println("Loading LocalWatchSystem "+i+"..");
		frame.hide();
		//LocalWatchSystem l = lws.get(i);
		new lwsGui().createGui(this);
	}

	public void addLWS(LWS l){
		lws.add(l);
	}
}
